package com.sikaryofficial.backend.config;

import cn.hutool.core.text.CharSequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @author : qinjinyuan
 * @desc : 邮件发送配置
 * @date : 2024/06/03 13:36
 */
@Configuration
public class EmailSenderConfig {
    @Value("${gmail.host}")
    private String gmailHost;
    @Value("${gmail.port}")
    private Integer gmailPort;
    @Value("${gmail.username}")
    private String gmailUsername;
    @Value("${gmail.password}")
    private String gmailPassword;
    @Value("${gmail.protocol}")
    private String gmailProtocol;
    @Autowired
    private EmailNormalConfig emailNormalConfig;

    @Bean("gmailSender")
    public JavaMailSenderImpl gmailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(gmailHost);
        mailSender.setPort(gmailPort);
        mailSender.setUsername(gmailUsername);
        mailSender.setPassword(gmailPassword);
        mailSender.setProtocol(gmailProtocol);

        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        mailSender.setJavaMailProperties(properties);
        return mailSender;
    }

    @Bean("normalSender")
    public JavaMailSenderImpl normalSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(emailNormalConfig.getHost());
        mailSender.setPort(emailNormalConfig.getPort());
        mailSender.setUsername(emailNormalConfig.getUsername());
        mailSender.setPassword(emailNormalConfig.getPassword());
        if (CharSequenceUtil.isNotBlank(emailNormalConfig.getProtocol())) {
            mailSender.setProtocol(emailNormalConfig.getProtocol());
        }
        return mailSender;
    }
}
